package com.camerareader.mvp;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.camerareader.mvp.login_page.LoginActivity;
import com.camerareader.upload_list.UploadService;
import com.camerareader.util.CommonUtil;
import com.fileoperation.CommonUtils;
import com.fileoperation.model.LogOutEvent;
import com.fileoperation.model.SaveUserInfoEvent;
import com.fileoperation.user.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2016-12-22.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected Intent serviceIntent;
    protected UploadService uploadService;
    private String BASE_TAG = "BaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        serviceIntent = new Intent(this, UploadService.class);
        Log.w(BASE_TAG, "onCreate: serviceIntent");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.w(BASE_TAG, "start: serviceIntent  =  " + serviceIntent);
        bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
        EventBus.getDefault().register(this);
    }

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            UploadService.MyBinder myBinder = (UploadService.MyBinder) service;
            uploadService = myBinder.getService();
            BaseActivity.this.onServiceConnected();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            uploadService = null;
            BaseActivity.this.onServiceDisconnected();
        }
    };

    protected abstract void onServiceConnected();
    protected abstract void onServiceDisconnected();

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(serviceConnection);
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onLogOutEvent(LogOutEvent event){
        System.out.println("收到登出事件");
//        EventBus.getDefault().removeStickyEvent(event);
//        Toast.makeText(this, "用户信息已过期，请重新登录！", Toast.LENGTH_SHORT).show();
//        UserInfo.getInstance().clearUserInfo(this);
//        LoginActivity.navigateLoginActivity(this);
//        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onSaveUserInfo(SaveUserInfoEvent event){
        System.out.println("收到保存用户事件");
        EventBus.getDefault().removeStickyEvent(event);
        UserInfo.getInstance().saveUserData(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


}
